// ActionScript file
import com.helloniu.fl.ByteWebHandler;
import com.helloniu.fl.upload.FileUploader;

import flash.events.Event;
import flash.net.FileFilter;
import flash.net.FileReference;
private var fullUri:String = "http://192.168.12.12:8080/SuperPresentation/file/upload2";

// Event handler function to print a message
// describing the selected Button control.
private function printMessage(event:*):void  {
  message.text += event + " pressed" + "\n";
}

private function printMessage3(event:Event):void  {
}

private function printMessage4(event:Event):void  {
    //var aa = (new Date()).time;
    //var bb = (new Date()).time;
    //printMessage(aa +"|"+ bb);
    
    // 3,431,840,869
    // 3,000,000,000
    // 2,999,999,999
	var blockSize:uint = parseInt(3000000000 / 3 + "");
	var startPos:uint = 0;
	var endPos:uint = 0;

	for(var i:int = 0; i < 3; i++) {
    	startPos = i* blockSize;
    	endPos = startPos + blockSize - 1; 
        
    	if(i == 2) {
        	endPos = 3000000000 - 1;
        }
        
    	printMessage("----------: " + i +"|"+ startPos +"|"+ endPos);
    }
	printMessage("----------: " + blockSize);    
}


//---------------------------------------------------------------
private var fileToUpload:FileReference = null;
private var fileUploader:FileUploader = null;
private var startTime:Number = 0;
private var endTime:Number = 0;
private var lastUploaded:Number = 0;
private var remainingTime:Number = 0;

/**
 * Select a file from the local file system.
 */
private function selectLocalFile(event:Event):void  {
	fileToUpload = new FileReference();
    fileToUpload.browse([new FileFilter("*.pdf;*.txt", "*.pdf;*.txt")]);
}

/**
 * UpLoad the file handler.
 */
private function uploadLocalFile(event:Event):void {
	printMessage("uploadLocalFile");
	fileUploader = new FileUploader();
	fileUploader.setUploadURL(fullUri);
	fileUploader.setFileReference(fileToUpload);
	fileUploader.setMultThreads(4);
	fileUploader.startUpload(uploadProgress);
}

private function uploadProgress(fileUploader:FileUploader):void {
	if(fileUploader.getState() != ByteWebHandler.COMMUNICATION_SUCCESSFUL) {
        // show error
        // alert?
    	return;
    }
    
	fileUploader.getUploadedTotal();
    /*
	printMessage("--------uploadProgress--: " + fileUploader.getState() +"|"+ 
	fileUploader.getFileLength() +"|"+ fileUploader.getUploadedTotal());
    */
	if(endTime == 0) {
    	endTime = (new Date()).time;
    }
	else {
    	endTime = (new Date()).time;
    	var timeE:uint = endTime - startTime;
        
    	if(lastUploaded > 0 && timeE > 0) {
        	var speed:Number = (fileUploader.getUploadedTotal() - lastUploaded) / timeE;
            
        	if(!isNaN(speed)) {
            	speed = parseInt((speed * 1000 /1024) + "");
                
            	remainingTime = fileUploader.getFileLength() - fileUploader.getUploadedTotal();
            	if(remainingTime > 0) {
                	remainingTime = parseInt(((remainingTime *1000)/ (1024*speed)) + "");
                }
                
            	printMessage("-speed:: " + speed + "KB/S" + " remainingTime: " + remainingTime + "ms");
            }
        }
    }
    
	if(lastUploaded == 0) {
    	lastUploaded = fileUploader.getUploadedTotal();
    }
    
	if(startTime == 0) {
    	startTime = (new Date()).time;
    }
	else {
    	startTime = (new Date()).time;
    }    
}

private function printMessageA(event:Event):void  {
	fileUploader.pause();
}

private function printMessageB(event:Event):void  {
	fileUploader.resume();
}
private function cancelUpload(event:Event):void{
    //删除未上传完成的文件，同时删除日志文件，并清除上传状态窗口内容
	fileUploader.cancel();
}


